package emil.poker;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

public class PlayLimitHoldem {
	
	LimitHoldem limitHoldem;
	Logger logger;
	
	public static void main(String[] args) throws Exception {
		Resource resource = new FileSystemResource( "src/emil/poker/beans.xml" );
		BeanFactory factory =
	         new XmlBeanFactory( resource );
				
		PlayLimitHoldem playLimitHoldem =
	         (PlayLimitHoldem) factory.getBean("playLimitHoldem");
		playLimitHoldem.getLimitHoldem().setBeanFactory( factory );
		playLimitHoldem.play();
	}
	
	public void play() throws Exception {
		logger.info( "Starta spel" );
		logger.info("---------------");
		
		boolean continuePlay = true;
		boolean isTrue = true;
		boolean isFirstGame = true;
		int numOfGamesPlayed = 0;
		while (isTrue){
			numOfGamesPlayed++;
			logger.info("*****************************************");
			logger.info("*****************************************");
			limitHoldem.init();
			if (!isFirstGame) {
				limitHoldem.nextIsDealer();
			}else{
				limitHoldem.setDealer( 0 );
				isFirstGame = false;
			}
			
			
			limitHoldem.payBlinds();
			limitHoldem.dealTwoCardsPerPlayer();
			
			limitHoldem.displayPlayersInfo();
			
			continuePlay = limitHoldem.handleBetting();
			
			if ( !continuePlay ) {
				limitHoldem.showDown();
				Thread.sleep(1000);
				continue;
			}
			Thread.sleep(500);
			limitHoldem.dealFlop();
			logger.info( "+++++++++++++++++++++++" );
			logger.info( "flop: " + limitHoldem.getTable().getCommunityCards() );
			logger.info( "+++++++++++++++++++++++" );
			Thread.sleep(500);
			
			continuePlay = limitHoldem.handleBetting();
			
			if ( !continuePlay ) {
				limitHoldem.showDown();
				continue;
			}
			Thread.sleep(500);
			limitHoldem.dealTurn();
			logger.info( "+++++++++++++++++++++++" );
			logger.info( "turn: " + limitHoldem.getTable().getCommunityCards() );
			logger.info( "+++++++++++++++++++++++" );
			Thread.sleep(500);
			
			continuePlay = limitHoldem.handleBetting();
			
			if ( !continuePlay ) {
				limitHoldem.showDown();
				continue;
			}
			Thread.sleep(500);
			limitHoldem.dealRiver();
			logger.info( "+++++++++++++++++++++++" );
			logger.info( "river: " + limitHoldem.getTable().getCommunityCards() );
			logger.info( "+++++++++++++++++++++++" );
			Thread.sleep(500);
			
			continuePlay = limitHoldem.handleBetting();
			Thread.sleep(500);
			limitHoldem.showDown();
			Thread.sleep(1500);
			logger.info("************************");
			
			continuePlay = true;
		}
	}

	public LimitHoldem getLimitHoldem() {
		return limitHoldem;
	}

	public void setLimitHoldem(LimitHoldem game) {
		this.limitHoldem = game;
	}

	public Logger getLogger() {
		return logger;
	}

	public void setLogger(Logger logger) {
		this.logger = logger;
	}
}
